package com.tony.mindwarning.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tony.mindwarning.pojo.Product;
import com.tony.mindwarning.pojo.ProductInfo;
import com.tony.mindwarning.pojo.User;
import com.tony.mindwarning.service.ProductService;
import com.tony.mindwarning.utils.MessageResults;
import com.tony.mindwarning.utils.PageUtils;
import com.upyun.Result;
import com.upyun.UpYunUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.*;

import static com.tony.mindwarning.utils.UpYunUtils.testSync;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: ProductController
 * @Auther: 皮蛋布丁
 * @Date: 2021/04/09/10:04
 * @Description: 商品的控制器
 */
@Controller
@Scope("prototype")
public class ProductController {

    @Resource
    ProductService productService;

    /**
    * @Description: addProduct 新增商品的方法(文件上传)
    * @Param: [product, file, response, request]
    * @return: void
    * @Author: 皮蛋布丁
    */
    @RequestMapping("/product_add")
    public void addProduct(HttpSession session, Product product, @RequestParam("file") MultipartFile file, HttpServletResponse response, HttpServletRequest request)throws Exception{
        response.setContentType("text/html;charset=utf-8");
        User currUser= (User)session.getAttribute("userLogin");
        System.out.println("新增的对象是:"+product);
        if(file.isEmpty()){
            response.getWriter().write("<script>alert('上传的图片不能为空!');location.href='page_product';</script>");
            return;
        }
        //上传的文件不为空
        String filename = file.getOriginalFilename();
        //获取文件的后缀
        String suffixName = filename.substring(filename.lastIndexOf("."));
        //生成一个新的文件名
        filename= UUID.randomUUID()+suffixName;
        System.out.println("要上传服务器的文件名是:"+filename);

        //上传又拍云
        byte [] byteArr=file.getBytes();
        Result upyunData = testSync(byteArr,filename);
        JSONObject photoMsg = JSONObject.parseObject(upyunData.getMsg());
        String photeUrl = "http://mind-warning.test.upcdn.net" + photoMsg.getString("url");

//        //获取文件上传的路径
//        File path=new File(ResourceUtils.getURL("classpath:").getPath());
//        File upload=new File(path.getAbsolutePath(),"/public/upload/"+filename);
//        file.transferTo(upload);
//        System.out.println("文件上传成功:"+upload.getAbsolutePath());

        System.out.println("文件上传成功，地址为："+photeUrl);
        product.setPimage(photeUrl);
        //设置预警值
        product.setPwarn(100);
        int count = productService.addProduct(product,currUser.getId());
        if(count>0){
            response.getWriter().write("<script>alert('新增成功!');location.href='product_page';</script>");
        }else{
            response.getWriter().write("<script>alert('新增失败!');location.href='product_page';</script>");
        }
    }

    /**
    * @Description: queryPage 商品分页
    * @Param: [pageIndex, pageSize, model]
    * @return: java.lang.String
    * @Author: 皮蛋布丁
    */
    @RequestMapping("/product_page")
    public String queryPage(@RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,
                            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize, Model model){
        //总条数
        Integer totalCount = productService.totalCount();
        Map<String,Object> map=new HashMap<String,Object>();
        //开始查询的位置
        map.put("startIndex",(pageIndex-1)*pageSize);
        map.put("pageSize",pageSize);
        //每页的数据
        List<Product> products = productService.getProductByPage(map);
        //封装数据
        PageUtils<Product> pageUtils=new PageUtils<Product>(pageIndex,pageSize,totalCount,products);
        System.out.println("分页工具类："+pageUtils);
        model.addAttribute("pageUtils",pageUtils);
        System.out.println(pageUtils);
        return "product";
    }

    /**
    * @Description: product_goupdate 跳转更新页面
    * @Param: [pid, model]
    * @return: java.lang.String
    * @Author: 皮蛋布丁
    */
    @RequestMapping("/product_goupdate")
    public String product_goupdate(Integer pid,Model model){
        Product product = productService.findById(pid);
        model.addAttribute("product",product);
        return "editProduct";
    }

    /**
    * @Description: updateProduct 更新商品的方法
    * @Param: [product, file, response, request]
    * @return: void
    * @Author: 皮蛋布丁
    */
    @RequestMapping("/product_update")
    public void updateProduct(HttpSession session,Product product, @RequestParam("file") MultipartFile file, HttpServletResponse response, HttpServletRequest request)throws Exception{
        response.setContentType("text/html;charset=utf-8");
        User currUser= (User)session.getAttribute("userLogin");
        System.out.println("要更新的对象是:"+product);
        if(file.isEmpty()==false){
            //上传的文件不为空
            String filename = file.getOriginalFilename();
            //获取文件的后缀
            String suffixName = filename.substring(filename.lastIndexOf("."));
            //生成一个新的文件名
            filename= UUID.randomUUID()+suffixName;
            System.out.println("要上传服务器的文件名是:"+filename);
            //上传又拍云
            byte [] byteArr=file.getBytes();
            Result upyunData = testSync(byteArr,filename);
            JSONObject photoMsg = JSONObject.parseObject(upyunData.getMsg());
            String photeUrl = "http://mind-warning.test.upcdn.net" + photoMsg.getString("url");

//            //获取文件上传的路径
//            File path=new File(ResourceUtils.getURL("classpath:").getPath());
//            File upload=new File(path.getAbsolutePath(),"/public/upload/"+filename);
//            file.transferTo(upload);
//            System.out.println("文件上传成功:"+upload.getAbsolutePath());
//            product.setPimage(filename);

            System.out.println("文件上传成功，地址为："+photeUrl);
            product.setPimage(photeUrl);
        }

        int count = productService.updateProduct(product,currUser.getId());
        if(count>0){
            response.getWriter().write("<script>alert('更新成功!');location.href='product_page';</script>");
        }else{
            response.getWriter().write("<script>alert('更新失败!');location.href='product_goupdate?pid='"+product.getPid()+";</script>");
        }
    }

    /**
    * @Description: product_delete 商品删除
    * @Param: [pid]
    * @return: com.tony.mindwarning.utils.MessageResults
    * @Author: 皮蛋布丁
    */
    @ResponseBody
    @RequestMapping("/product_delete")
    public MessageResults product_delete(HttpSession session,Integer pid){
        User currUser= (User)session.getAttribute("userLogin");
        Integer count = productService.deleteProduct(pid,currUser.getId());
        MessageResults results=null;
        if(count>0){
            results=new MessageResults(200,"删除成功",null);
        }else{
            results=new MessageResults(500,"删除失败",null);
        }
        return results;
    }

    /**
    * @Description: product_infos 商品统计页面
    * @Param: [model]
    * @return: java.lang.String
    * @Author: 皮蛋布丁
    */
    @RequestMapping("/product_infos")
    public String product_infos(Model model) throws Exception {
        List<String> types=new ArrayList<String>();
        List<ProductInfo> productInfos = productService.getProductInfos();
        for (ProductInfo productInfo : productInfos) {
            types.add(productInfo.getName());
        }
        //把集合转为JSON格式的数据
        ObjectMapper mapper = new ObjectMapper();
        String jsonInfo = mapper.writeValueAsString(productInfos);
        System.out.println(jsonInfo);
        String typesJson = mapper.writeValueAsString(types);
        System.out.println(typesJson);
        model.addAttribute("jsonInfo",jsonInfo);
        model.addAttribute("typesJson",typesJson);
        return "productInfo";
    }

    /**
    * @Description: productsByTid 根据分类ID查询商品的集合
    * @Param: [tid]
    * @return: java.util.List<com.tony.mindwarning.pojo.Product>
    * @Author: 皮蛋布丁
    */
    @ResponseBody
    @RequestMapping("/productsByTid")
    public List<Product> productsByTid(Integer tid){
        return productService.productsByTid(tid);
    }



}
